#!/usr/bin/env python

"""
The format of the table:
Team       | A    | B         | ... | X |
Columbia 0 | 4:20 | 0:19 + 20 | ... |   |
"""

import csv, sys, re
from common import read_table
from optparse import OptionParser

def get_submission_times_0(s):
    if len(s.strip()) == 0:
        return [], []
    # "0:19 + 20" or "0:19"
    ma = re.search("(?P<hh>\\d+):(?P<mm>\\d+)\\s*(?:[+]\\s*(?P<penalty>\\d+))?", s)
    if ma is None:
        print >> sys.stderr, "[Warning] Cannot parse the time \"" + s + "\""
        return [], []
    sub_time = int(ma.group('hh')) * 60 + int(ma.group('mm'))
    n_wrongs = 0
    if not ma.group('penalty') is None:
        n_wrongs = int(ma.group('penalty')) / 20
    return [sub_time], [sub_time for i in range(n_wrongs)]

def get_submission_times_1(s):
    s = s.strip()
    if len(s) == 0 or s[0] == '(':
        return [], []
    # 209+20
    ma = re.search("(\\d+)(?:\+(\\d+))?", s)
    if ma is None:
        print >> sys.stderr, "[Warning] Cannot parse the time \"" + s + "\""
        return [], []
    sub_time = int(ma.group(1))
    n_wrongs = 0
    if not ma.group(2) is None:
        n_wrongs = int(ma.group(2)) / 20
    return [sub_time], [sub_time for i in range(n_wrongs)]

def get_submission_times_2(s):
    s = s.strip()
    # 1/217 or 5/-- or 1 217 or 5 --
    ma = re.search("(\\d+)[\\s+/](\\S+)", s)
    if ma is None:
        print >> sys.stderr, "[Warning] Cannot parse the time \"" + s + "\""
        return [], []
    # No correct submissions
    if ma.group(2) == "--":
        return [], []
    n_wrongs = int(ma.group(1)) - 1
    sub_time = int(ma.group(2))
    return [sub_time], [sub_time for i in range(n_wrongs)]

def format_time(mm):
    hh = mm / 60
    mm = mm % 60
    return "%02d:%02d"%(hh, mm)

if __name__ == "__main__":
    
    parser = OptionParser(usage = "%prog [options] < <Scoreboard in CSV format>")
    parser.add_option("-t", "--time-format",
            action = "store",
            type = "int",
            dest = "t",
            default = 0,
            help = "0: 0:19 + 20; 1: 209 + 20; 2: 1/217 or 1 217")
    (options, args) = parser.parse_args()

    if len(args) != 0:
        parser.print_usage()
        exit(1)

    a, m, n = read_table(sys.stdin)
    prob_id = 'A'
    for j in range(1, n):
        print >> sys.stderr, "Processing the submissions for Problem", prob_id
        for i in range(1, m):
            team_name = a[i][0].strip()
            if options.t == 0:
                right, wrong = get_submission_times_0(a[i][j])
            elif options.t == 1:
                right, wrong = get_submission_times_1(a[i][j])
            elif options.t == 2:
                right, wrong = get_submission_times_2(a[i][j])
            else:
                # unreachable
                assert False
            for sub_time in wrong:
                if sub_time > 0:
                    sub_time -= 1;
                print format_time(sub_time) + "," + team_name + "," + prob_id + ",,;W"
            for sub_time in right:
                print format_time(sub_time) + "," + team_name + "," + prob_id + ",,;A"
        prob_id = (chr)(ord(prob_id) + 1)
